package com.flyou.ccnucenter.ui.activity.mine;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.LoveRecycleViewAdapter;
import com.flyou.ccnucenter.domain.AnonymousLove;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.activity.henuBeside.anonymousLove.AddLoveActivity;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.LoginUtils;
import com.flyou.ccnucenter.widget.loading.LoadingView;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

public class MyLoveActivity extends BaseActivity1 implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private LoadingView loadingView;
    private RelativeLayout failedView;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout mSwipeLayout;
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private RelativeLayout getmTitlebarRlRight;
    private ImageView title_right_imgae;
    private String userId;
    private LoveRecycleViewAdapter adapter;
    private LinearLayoutManager linearLayoutManager;

    private int currentPage = 0;
    private int pageSize = 10;
    private List<AnonymousLove> loveList;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_anonymous_love_list);

        loadingView = (com.flyou.ccnucenter.widget.loading.LoadingView) findViewById(R.id.loadingView);
        loadingView.setLoadingText("正在扫描表白墙……");
        loadingView.setVisibility(View.VISIBLE);

        failedView = (RelativeLayout) findViewById(R.id.failed_view);


        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);


        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        getmTitlebarRlRight = (RelativeLayout) findViewById(R.id.titlebar_rl_Right);
//        getmTitlebarRlRight.setVisibility(View.VISIBLE);
        title_right_imgae = (ImageView) findViewById(R.id.iv_right_image);
        title_right_imgae.setImageResource(R.drawable.note_add_selcetor);
        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);

        //        recyclerView.addItemDecoration(new RecycleViewItemDecoration(NoteListActivity.this, RecycleViewItemDecoration.VERTICAL_LIST));

    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("我的表白");
        userId = LoginUtils.getUserId(MyLoveActivity.this);
        //        addNote();
        GetLoveFromNet();
    }

    /*
    *从服务器获取笔记数据
    */
    private void GetLoveFromNet() {
        MyUser currentUser = BmobUser.getCurrentUser(MyLoveActivity.this, MyUser.class);
        BmobQuery<AnonymousLove> query = new BmobQuery<AnonymousLove>();
        query.order("-createdAt");//根据更新时间排序
        query.addWhereEqualTo("myUser",currentUser );
        query.setLimit(pageSize);//每页显示数据
        query.setSkip(currentPage * pageSize);//分页

        query.findObjects(this, new FindListener<AnonymousLove>() {
            @Override
            public void onStart() {
                if (failedView.isShown()) {
                    failedView.setVisibility(View.GONE);
                }
                super.onStart();
            }

            @Override
            public void onFinish() {
                if (mSwipeLayout.isRefreshing()) {
                    mSwipeLayout.setRefreshing(false);
                }
                if (loadingView.isShown()) {
                    loadingView.setVisibility(View.GONE);
                }
                super.onFinish();
            }

            @Override
            public void onSuccess(List<AnonymousLove> object) {

                if (currentPage == 0) {
                    loveList = object;
                    adapter = new LoveRecycleViewAdapter(MyLoveActivity.this, object);
                    /*
                    * item 动画
                    * */
                    recyclerView.setItemAnimator(new SlideInLeftAnimator());
                    /*
                    * recycleview动画
                    * */

                    recyclerView.setAdapter(new ScaleInAnimationAdapter(adapter));

                    recyclerView.addOnScrollListener(new onRecycleViewScollListener());

                    adapter.setOnItemClickLitener(new onItiemClickListener());
                } else {
                    if (object.size() == 0) {

                        return;
                    }
                    loveList.addAll(object);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (currentPage == 0) {
                    failedView.setVisibility(View.VISIBLE);
                }
                Toast.makeText(MyLoveActivity.this, BmobErrorCodeUtils.getMessage(code, msg), Toast.LENGTH_SHORT).show();
            }
        });

    }


    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        getmTitlebarRlRight.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.titlebar_rl_back:
                finish();
                break;
            case R.id.titlebar_rl_Right:
                if (LoginUtils.isLogined(MyLoveActivity.this)) {
                    Intent intent = new Intent(MyLoveActivity.this, AddLoveActivity.class);
                    startActivityForResult(intent, 1);
                } else {
                    LoginUtils.Login(MyLoveActivity.this);
                }


                break;

        }
    }

    @Override
    public void onRefresh() {
        currentPage = 0;
        GetLoveFromNet();
    }

    /*
    * 滑动事件的监听
    * */
    public class onRecycleViewScollListener extends RecyclerView.OnScrollListener {
        private int lastVisibleItem = 0;
        private boolean isSlidingToLast = false;

        public onRecycleViewScollListener() {
            super();
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

            super.onScrollStateChanged(recyclerView, newState);
            if (isSlidingToLast) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    currentPage++;
                    GetLoveFromNet();
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                isSlidingToLast = true;
            } else {
                isSlidingToLast = false;
            }
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }

    public class onItiemClickListener implements LoveRecycleViewAdapter.OnItemClickLitener {
        @Override
        public void onItemClick(View view, int position) {

        }

        @Override
        public void onItemLongClick(View view, final int position) {
//            final AnonymousLove loves = loveList.get(position);
//            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AnonymousLoveListActivity.this);
//            alertDialog.setTitle("删除笔记？");
//            alertDialog.setMessage("您确定删除笔记吗？");
//            alertDialog.setNegativeButton("我再想想", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                }
//            });
//            alertDialog.setPositiveButton("必须的", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    Note note = new Note();
//                    note.setObjectId(loves.getObjectId());
//                    note.delete(AnonymousLoveListActivity.this, new DeleteListener() {
//                        @Override
//                        public void onSuccess() {
//                            adapter.removeItem(position);
//                        }
//
//                        @Override
//                        public void onFailure(int i, String s) {
//                            Toast.makeText(AnonymousLoveListActivity.this, BmobErrorCodeUtils.getMessage(i, s), Toast.LENGTH_SHORT).show();
//                        }
//                    });
//
//                }
//            });
//            alertDialog.show();
        }
    }


}
